While Loops

Objectives


Discussion

Looping

Example

Private Function IsPrime(ByVal iNum As Int16) As Boolean
   If (iNum = 2 Or iNum = 1) Then Return False
   Dim i As Int16 = 2
   Dim bPrime As Boolean = True
   Do
      If iNum Mod i = 0 Then bPrime = False
      i = i + 1
   Loop Until bPrime = False Or i = iNum
   Return bPrime
End Function

Carriage Return

messagebox.show("hi" & vbcrlf & "guys" & chr(13) & "and girls")

Back to top


Demonstration

1. In this demo we will write a function that requests a number from the user.  The function will keep asking for a number until the user enters a number.

2.  Open your Unit4 project and add a new form.  On this form add a button (btnGetNumber).

3.  Add the following code to the click event for your button:

Dim sNum As String
Do
   sNum = InputBox("Enter a number", "Number")
Loop While IsNumeric(sNum) = False

4.  Run the program and enter numbers and letters.  Also trace through the program to observe the flow.

5.  We will continue experimenting with loops. One of the most common mistakes is to create a loop that will never terminate.  When this happens your program "hangs" and does not end.  We will demonstrate.  Add another button (btnEndless) and add the following code to its click event:

Dim i, j As Int16
Dim sTable As String
i = 1
Do While (i < 10)
   j = 1
   Do While (j < 10)
      sTable = sTable & i * j & " "
      j = j + 1
   Loop 
   sTable = sTable & vbCrLf
   i = i + 1
Loop
MessageBox.Show(sTable)

6.  Run the program and click the button.  Your program should not hang at this point.  Hopefully you are seeing a very crude multiplication table in a messagebox.  Before we make the program hang check out the code.  Notice the statement "vbCrLf".  This is a special constant that stands for "Carriage return, line feed".  This constant is like hitting the enter key and is what causes the text to be on different lines.  You could also use chr(13).  The function chr() returns the character corresponding to the ASCII code that is passed into it.  For more information about ASCII codes check out the help file referenced in the section Links & Help .   

7.  Study the code and understand what it does.  Now we are going to make the program hang.  Comment out the line "j = j + 1".  Put a break next to the click event for the button.  Now step through the program using F8.  Notice that the value of j never changes so the inner loop will never exit. If you run the program without the break you will probably have to use ctrl-alt-delete to end your program.

Back to top


Exercises

1. Use the IsPrime function and a one of the loops discussed in this lesson to determine all prime numbers between 1 and 1000.  Use whatever approach you want to display these numbers.  Notice that it may make more sense to use a for loop. 

2.  Create a program that keeps asking the user to enter a prime number unitl the user enters one.  The program should meet the following requirements:

  1. It has a button.  When the clicked an InputBox requests the user to enter a number.
  2. The program keeps displaying the InputBox until the user enters a prime number.  Be careful, you should also ensure that the user enters a number before calling IsPrime since IsPrime is expecting a number.

3.  Someone makes you the following offer. They will give you either (1)$1,000,000 or (2) the number of pennies that you would have if you start with one penny on day 1 and double the number of pennies every day for 30 days until you reach day 31.  You need to determine what is the better offer.  So that you can use one of the loops in this lesson you should write a program that determines the number of pennies that you will have each day from day 1until the day that the number of pennies exceeds 100,000,000 (which is $1,000,000).  Your program should assemble a table (which is just a string with some vbcrlf's) that lists the day number and the number of pennies.  Your table should look like the following except that it should stop when the number of pennies exceeds 100,000,000:

1    1
2    2
3    4
4    8
5   16

4.  If you were to repeat #3 but have the table stop on day 31, what type of loop may make more sense to use?

Back to top
Links & Help
Back to top